<?php

/**
 * contact : 刘经理 19326416371
 * company : 版权所属 四川北星惯牌科技有限公司
 **/

namespace app\api\controller;

use app\api\validate\CaptainTeam;
use app\common\controller\Api;
use app\common\manager\BaseManager;
use app\common\manager\CaptainManager;
use app\common\manager\EmployeeManager;
use Monolog\Handler\IFTTTHandler;
use think\Config;
use think\Exception;

/**
 * 队长中心
 */
class Employee extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();

        if (!Config::get('fastadmin.usercenter')) {
            $this->error(__('User center already closed'));
        }

    }

    /**
     * 队长个人信息及中心信息
     */
    public function index()
    {
        try {

        }catch (Exception $e){
            $this->error($e->getMessage());
        }
    }

    public function employee()
    {
        $user = $this->user;
        $storeId = $this->request->post('store_id');
        $mobile = $this->request->post('mobile');
        $captcha = $this->request->post('captcha');
        $real_name = $this->request->post('real_name');
        $is_group = $this->request->post('is_group') ?? 0;
        $is_write = $this->request->post('is_write') ?? 0;
        $is_assistant = $this->request->post('is_assistant') ?? 0;
        $level = $this->request->post('level') ?? 0;
        $data = [
            'mobile' => $mobile,
            'real_name' => $real_name,
            'is_group' => $is_group,
            'is_write' => $is_write,
            'captcha' => $captcha,
            'is_assistant' => $is_assistant,
            'level' => $level
        ];
        $employeeValidate = new \app\api\validate\Employee();
        if (!$employeeValidate->check($data)) {
            $this->error($employeeValidate->getError());
        }

        try {
            $result = EmployeeManager::employee($user,$data,$storeId);
            if ($result['code']==BaseManager::$successCode){
                $this->success($result['message']);
            }else{
                $this->error($result['message']);
            }
        }catch (Exception $e){
            $this->error($e->getMessage());
        }

    }

    public function modify()
    {
        $user = $this->user;
        $storeId = $this->request->post('store_id');
        $mobile = $this->request->post('mobile');
        $real_name = $this->request->post('real_name');
        $is_group = $this->request->post('is_group');
        $is_write = $this->request->post('is_write');
        $is_assistant = $this->request->post('is_assistant') ?? 0;
        $level = $this->request->post('level') ?? 0;
        $id = $this->request->post('id');

        $data = [
            'mobile' => $mobile,
            'real_name' => $real_name,
            'is_group' => $is_group,
            'is_write' => $is_write,
            'is_assistant' => $is_assistant,
            'level' => $level,
        ];
        $employeeValidate = new \app\api\validate\EmployeeModify();
        if (!$employeeValidate->check($data)) {
            $this->error($employeeValidate->getError());
        }

        try {
            $result = EmployeeManager::modify($user,$id,$data,$storeId);
            if ($result['code']==BaseManager::$successCode){
                $this->success($result['message']);
            }else{
                $this->error($result['message']);
            }
        }catch (Exception $e){
            $this->error($e->getMessage());
        }
    }

    public function destroy()
    {
        $user = $this->user;
        $id = $this->request->post('id');
        try {
            $result = EmployeeManager::destroy($user,$id);
            if ($result['code']==BaseManager::$successCode){
                $this->success($result['message']);
            }else{
                $this->error($result['message']);
            }
        }catch (Exception $e){
            $this->error($e->getMessage());
        }
    }

    public function list()
    {
        $user = $this->user;
        $id = $this->request->param('id');
        $page = $this->request->param('page');
        try {
            $result = EmployeeManager::list($user,$id,$page);
            if ($result['code']==BaseManager::$successCode){
                $this->success($result['message'],$result['data']);
            }else{
                $this->error($result['message']);
            }
        }catch (Exception $e){
            $this->error($e->getMessage());
        }

    }
}
